<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>

    <div>请输入要猜的数字</div>
    <input type="text">
    <button>提交</button>

    <div class="result"></div>

    <script>
        //1.生成一个1~100的随机整数
        let toGuess = parseInt(100*Math.random())+1;
        console.log(toGuess);

        //2.进行猜数字操作
        let  button = document.querySelector('button');
        let input = document.querySelector('input');
        let resultDiv = document.querySelector('.result');

        button.onclick = function(){
            //3.取出输入框中的内容
            if(input.value == ''){
                return;
            }

            let inputNum = parseInt(input.value);

            //进行比较大小关系
            if(inputNum < toGuess){

                resultDiv.innerHTML='低了';
            }else if(inputNum > toGuess){

                resultDiv.innerHTML='高了';
            }else{

                resultDiv.innerHTML='恭喜你答对了！';
            }
        }
    </script>
</body>
</html>